
import codeanticode.gsvideo.GSMovie;
import processing.core.*;
import unplayer.youtubeplayer.util.*;
import unplayer.youtubeplayer.*;
import unplayer.videoplayer.*;


  int window = 480; 
  int widthPlayer = 854;
  int heigthPlayer = 525;

  public void setup() {

    size(854,545,P2D);

    player = new VideoPlayerYoutube(this);

    font = createFont("Verdana", 12);

    sizeNum=window;

    url="";
  }

  public void movieEvent(GSMovie myMovie) {
    myMovie.read();
  }


  public void draw() {
    background(0);
    buttonPress=0; // NO BUTTON PRESS

    //BUTTON LOAD
    if (mouseX >= 0 && mouseX <= 100
      && mouseY >= 0&& mouseY <= titleHeigth) {

      if (mousePressed) {
        fill(220);
        buttonPress=1;
      }
      else
      {
        fill(205);
      }
    }
    else
    {
      fill(180);
    }

    stroke(0);
    rect(0,0,buttonLoadWth,titleHeigth);

    fill(0);
    noStroke();
    textFont(font);
    text("Paste & Load",9,16);

    //DIRECTION BAR

    fill(230);
    stroke(0);
    rect(buttonLoadWth,0,width-buttonLoadWth-sizeWth-buttonArrowWth,titleHeigth);

    fill(0);
    noStroke();
    textFont(font);
    text(url,buttonLoadWth+6,16);

    //SIZE BAR

    fill(205);
    stroke(0);
    rect(width-sizeWth-buttonArrowWth,0,sizeWth,titleHeigth);

    int delay; //DELAY TEXT
    if(sizeNum==1080) delay=5;
    else if(sizeNum==0)delay=3;
    else delay=9;

    String strSize; //STRING TEXT

    if(sizeNum>0) strSize=sizeNum+"p";
    else strSize="Default";

    fill(0);
    noStroke();
    textFont(font);
    text(strSize,width-sizeWth-buttonArrowWth+delay,16);

    //BUTTON UP

    if (mouseX >= width-buttonArrowWth && mouseX <= width
      && mouseY >= 0&& mouseY <= titleHeigth/2) {

      if (mousePressed) {
        fill(220);
        buttonPress=2;
      }
      else
      {
        fill(205);
      }
    }
    else {
      fill(180);
    }

    stroke(0);
    rect(width-buttonArrowWth,0,buttonArrowWth,titleHeigth/2);

    fill(0);
    noStroke();
    triangle(width-buttonArrowWth+7,titleHeigth/2-2,
    width-buttonArrowWth+(buttonArrowWth/2),2,
    width-7,titleHeigth/2-2);

    //BUTTON DOWN

    if (mouseX >= width-buttonArrowWth && mouseX <= width
      && mouseY >= titleHeigth/2 && mouseY <= titleHeigth) {

      if (mousePressed) {
        fill(220);
        buttonPress=3;
      }
      else
      {
        fill(205);
      }
    }
    else {
      fill(180);
    }

    stroke(0);
    rect(width-buttonArrowWth,titleHeigth/2,buttonArrowWth,titleHeigth/2);

    fill(0);
    noStroke();
    triangle(width-buttonArrowWth+7,titleHeigth/2+3,
    width-buttonArrowWth+(buttonArrowWth/2),titleHeigth-1,
    width-7,titleHeigth/2+3);
    
    player.video(0,titleHeigth,widthPlayer,heigthPlayer);
  }

  void paste()
  {
    url = Tools.getClipBoard();
    if(!url.equals("")) loadVideo();
  }

  void changeSizeUp()
  {
    if(sizeNum==0) sizeNum=240;
    else if(sizeNum==240) sizeNum=360;
    else if(sizeNum==360) sizeNum=480;
    else if(sizeNum==480) sizeNum=720;
    else if(sizeNum==720) sizeNum=1080;
    else if(sizeNum==1080) sizeNum=0;

    if(!url.equals("")) loadVideo();
  }

  void changeSizeDown()
  {
    if(sizeNum==0) sizeNum=1080;
    else if(sizeNum==240) sizeNum=0;
    else if(sizeNum==360) sizeNum=240;
    else if(sizeNum==480) sizeNum=360;
    else if(sizeNum==720) sizeNum=480;
    else if(sizeNum==1080) sizeNum=720;

    if(!url.equals("")) loadVideo();
  }

  void loadVideo()
  {
    int sizeLocal=0;

    if(sizeNum==0) sizeLocal=0;
    else if(sizeNum==240) sizeLocal=5;
    else if(sizeNum==360) sizeLocal=34;
    else if(sizeNum==480) sizeLocal=35;
    else if(sizeNum==720) sizeLocal=22;
    else if(sizeNum==1080) sizeLocal=37;

    player.loadVideo(url,sizeLocal);

    if(!UrlStream.isValidUrl(url)) url="";
  }

  public void mouseReleased()
  {
    if(buttonPress==1) paste();
    if(buttonPress==2) changeSizeUp();
    if(buttonPress==3) changeSizeDown();
  }



  VideoPlayerYoutube player;
  PFont font;
  int titleHeigth = 20;
  int buttonLoadWth = 100;
  int sizeWth = 50;
  int buttonArrowWth = 20;
  String url;
  int sizeNum;
  int buttonPress; //1 LOAD   2 UP  3 DOWN

